/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import java.util.Collection;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.constants.CommonConstants;
import com.lmx.persistence.service.RoleService;
import com.lmx.repoDomain.exception.BusinessException;
import com.lmx.repoDomain.tmodel.Privilege;
import com.lmx.repoDomain.tmodel.Role;//hubiati
import com.lmx.repoDomain.tmodel.Tenant;

import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;
import java.util.Iterator;

@JsonApiResourceRepository(Role.class)
@Component
public class RoleRepository extends BaseRepository<Role> {

	@Autowired
	protected RoleService roleService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(Role.class);
	}

	@Override
	@JsonApiSave
	public Role save(Role record) {
		if (record.getId() == null) {
			Role temp = roleService.getRoleByName(record.getName());
			if (temp != null) {
				record.setErrcode(4);
				throw new BusinessException(null, CommonConstants.ERROR_CODE.NAME_ERROR.label(), null);
			} else {
				record.setType(2);// 待定
//				if(record.getCode()==null||"".equals(record.getCode())){
//					record.setCode("normal");
//				}
				Collection<Privilege>  pList=record.getPrivileges();
				Iterator<Privilege> it = pList.iterator();
				while(it.hasNext()){
				    Privilege p = it.next();
				    Tenant t = new Tenant();
				    t.setId(111L);
				    p.setTenant(t);
				}
				dslJpaService.create(record);
			}
		} else {
			if (record.getDelStatus() == 1) {
				dslJpaService.update(record);
			} else {
				int updateFlag = 1;
//				Role oldData = roleService.getRoleById(record.getId());
//				if (oldData.getName().equals(record.getName())) {
//
//				} else {
					Role temp = roleService.getRoleByNameFilterId(record.getName(),record.getId());
					if (temp != null) {
						updateFlag = 0;
					}
//				}
				if (updateFlag == 1) {
					dslJpaService.update(record);
				} else {
					record.setErrcode(4);
					throw new BusinessException(null, CommonConstants.ERROR_CODE.NAME_ERROR.label(), null);
				}

			}
		}
		//如果赋予的权限里包含移动端的权限，则需要把实际移动端权限放入到角色权限表中
		boolean hasMobilePrivilege = false;
		Iterator<Privilege> it = record.getPrivileges().iterator();
		while(it.hasNext()){
		    Privilege p = it.next();
		    if(p.getType().intValue()==0){
		        hasMobilePrivilege = true;
		        break;
		    }
		}
		if(hasMobilePrivilege){
		    this.roleService.buildMobilePrivileges(record);
		}
		return record;
	}
}
